package com.xuwei.base.thread;

import java.util.Timer;
import java.util.TimerTask;

import org.junit.Test;
/**
 * 传统方式创建线程
 * @author David
 * since 2015年6月7日 下午12:41:41
 * @version 
 * @since JDK 1.6
 */
public class TraditionalThread {
	public void test1() {
		new Thread() {
			public void run() {
				while (true) {
					try {
						Thread.sleep(2000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					System.out.println(Thread.currentThread().getName());
				}
			}
		}.start();

	}
   
	public void test2() {
		new Thread(new Runnable() {
			public void run() {
				while (true) {
					try {
						Thread.sleep(2000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					System.out.println(Thread.currentThread().getName());
				}
			}
		}).start();
	}

	/*
	 * 传统的定时器实现方式
	 */
	public void test3() {

		new Timer().schedule(new TimerTask() {
			public void run() {
				System.out.println(Thread.currentThread().getName());
			}
		}, 10000, 1000);
	}
	
	public static void main(String[] args) {
		new TraditionalThread().test1();
	}

}
